<template>
    <el-container>
        <el-header>
            <headers :activeIndex="activeIndex"></headers>
        </el-header>
        <el-main>

            <el-row>
                <el-col :offset="3" :span="18" style="margin-top:2rem;">

                    <el-row>
                        <el-col :offset="22" :span="2">
                            <el-button @click="addOne">新增</el-button>
                        </el-col>
                    </el-row>

                    <el-row style="margin-top:2rem;">
                        <el-card class="box-card" shadow="never">
                            <div slot="header" class="clearfix">
                                <span>轮播图</span>
                            </div>
                            <el-table :data="rotationData" style="width: 100%">
                                <el-table-column prop="id" label="id" width="180">
                                </el-table-column>
                                <el-table-column prop="type" label="类型" width="180">
                                </el-table-column>
                                <el-table-column prop="commodityId" label="商品id" width="400">
                                </el-table-column>
                                <el-table-column prop="name" label="商品名" width="180">
                                </el-table-column>
                                <el-table-column  label="图片" width="180">
                                    <template slot-scope="scope">
                                        <el-image style="width: 100px;margin-top:10px;" v-show="scope.pic!=''" :src="scope.row.pic"></el-image>
                                    </template>  
                                </el-table-column>
                                <el-table-column prop="mark" label="分值" width="180">
                                </el-table-column>
                                <el-table-column label="操作">
                                    <template slot-scope="scope">
                                        <el-button size="mini" @click="handleEdit(scope.row)">编辑
                                        </el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </el-row>

                    <el-row style="margin-top:2rem;">
                        <el-card class="box-card" shadow="never">
                            <div slot="header" class="clearfix">
                                <span>广告</span>
                            </div>
                            <el-table :data="normalData" style="width: 100%">
                                <el-table-column prop="id" label="id" width="180">
                                </el-table-column>
                                <el-table-column prop="type" label="类型" width="180">
                                </el-table-column>
                                <el-table-column prop="commodityId" label="商品id" width="400">
                                </el-table-column>
                                <el-table-column prop="name" label="商品名" width="180">
                                </el-table-column>
                                <el-table-column label="图片" width="180">
                                    <template slot-scope="scope">
                                        <el-image style="width: 100px;margin-top:10px;" v-show="scope.pic!=''" :src="scope.row.pic"></el-image>
                                    </template> 
                                </el-table-column>
                                <el-table-column prop="mark" label="分值" width="180">
                                </el-table-column>
                                <el-table-column label="操作">
                                    <template slot-scope="scope">
                                        <el-button size="mini" @click="handleEdit(scope.row)">编辑
                                        </el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </el-row>

                    <el-row style="margin-top:2rem;">
                        <el-card class="box-card" shadow="never">
                            <div slot="header" class="clearfix">
                                <span>热卖区</span>
                            </div>
                            <el-table :data="bestsellData" style="width: 100%">
                                <el-table-column prop="id" label="id" width="180">
                                </el-table-column>
                                <el-table-column prop="type" label="类型" width="180">
                                </el-table-column>
                                <el-table-column prop="commodityId" label="商品id" width="400">
                                </el-table-column>
                                <el-table-column prop="name" label="商品名" width="180">
                                </el-table-column>
                                <el-table-column label="图片" width="180">
                                    <template slot-scope="scope">
                                        <el-image style="width: 100px;margin-top:10px;" v-show="scope.pic!=''" :src="scope.row.pic"></el-image>
                                    </template> 
                                </el-table-column>
                                <el-table-column prop="mark" label="分值" width="180">
                                </el-table-column>
                                <el-table-column label="操作">
                                    <template slot-scope="scope">
                                        <el-button size="mini" @click="handleEdit(scope.row)">编辑
                                        </el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </el-row>

                    <el-row style="margin-top:2rem;">
                        <el-card class="box-card" shadow="never">
                            <div slot="header" class="clearfix">
                                <span>精品区</span>
                            </div>
                            <el-table :data="elaborateData" style="width: 100%">
                                <el-table-column prop="id" label="id" width="180">
                                </el-table-column>
                                <el-table-column prop="type" label="类型" width="180">
                                </el-table-column>
                                <el-table-column prop="commodityId" label="商品id" width="400">
                                </el-table-column>
                                <el-table-column prop="name" label="商品名" width="180">
                                </el-table-column>
                                <el-table-column label="图片" width="180">
                                    <template slot-scope="scope">
                                        <el-image style="width: 100px;margin-top:10px;" v-show="scope.pic!=''" :src="scope.row.pic"></el-image>
                                    </template> 
                                </el-table-column>
                                <el-table-column prop="mark" label="分值" width="180">
                                </el-table-column>
                                <el-table-column label="操作">
                                    <template slot-scope="scope">
                                        <el-button size="mini" @click="handleEdit(scope.row)">编辑
                                        </el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </el-row>


                </el-col>
            </el-row>

            <el-dialog title="新增或编辑" :visible.sync="dialogVisible" width="100%">
                <el-row>
                    <el-col :offset="4" :span="16">
                        <el-row>
                            <el-col :span="4">
                                商品id
                            </el-col>
                            <el-col :span="20">
                                <el-input v-model="commodityId"></el-input>
                            </el-col>
                        </el-row>
                        <el-row style="margin-top:1rem;">
                            <el-col :span="4">
                                类型
                            </el-col>
                            <el-col :span="20">
                                <el-select v-model="type" placeholder="请选择">
                                    <el-option v-for="item in types" :key="item.value" :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-col>
                        </el-row>
                        <el-row style="margin-top:1rem;"> 
                            <el-col :span="4">
                                商品名
                            </el-col>
                            <el-col :span="20">
                                <el-input v-model="name" placeholder="5个字以内"></el-input>
                            </el-col>
                        </el-row>
                        <el-row style="margin-top:2rem;">
                            <el-col :span="4">
                                图片
                            </el-col>
                            <el-col :span="20">
                                <el-row>
                                    <el-col :span="2">
                                        <el-upload
                                        :action="this.$store.state.upload_base_url+'/api/upload'"
                                        :on-success="handlePreview">
                                        <el-button size="small" type="primary">点击上传</el-button>
                                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                                        </el-upload>
                                    </el-col>
                                    <el-col :span="22">
                                        <el-input v-model="pic"></el-input>
                                    </el-col>
                                </el-row>
                                <el-image style="width: 100px;margin-top:10px;" v-show="pic!=''" :src="pic"></el-image>                
                            </el-col>
                        </el-row>
                        <el-row style="margin-top:2rem;">
                            <el-col :span="4">
                                分值
                            </el-col>
                            <el-col :span="20">
                                <el-input v-model="mark"></el-input>
                            </el-col>
                        </el-row>
                    </el-col>
                </el-row>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="dosubmit">确 定</el-button>
                </span>
            </el-dialog>

        </el-main>
    </el-container>
</template>

<script>
    import headers from '@/components/headers.vue'
    import axios from 'axios'
    export default {
        data: function () {
            return {
                activeIndex: '5',
                dialogVisible: false,
                insert: 0,
                types: [{
                        value: "rotation",
                        label: "轮播图"
                    },
                    {
                        value: "normal",
                        label: "广告"
                    },
                    {
                        value: "bestsell",
                        label: "热卖区"
                    },
                    {
                        value: "elaborate",
                        label: "精品区"
                    },
                ],

                rotationData: [],
                normalData: [],
                bestsellData: [],
                elaborateData: [],

                id: '',
                type: "",
                name:"",
                pic: "",
                commodityId: "",
                mark: ""
            }
        },
        components: {
            headers
        },
        methods: {
            handleEdit(row) {
                this.id = row.id
                this.type = row.type
                this.name=row.name
                this.pic = row.pic
                this.commodityId = row.commodityId
                this.mark = row.mark


                this.dialogVisible = true
                this.insert = 0
            },
            handleDelete(id) {
                axios.delete(this.$store.state.base_url + '/ad/' + id).then(res => {
                    if (res.data.code == "SUCCESS") {
                        this.dialogVisible = false
                        this.getalldata()
                    } else {
                        this.$notify.error({
                            title: '修改失败',
                            message: this.$createElement('i', {
                                style: 'color: teal'
                            }, res.data.message),
                        });
                    }
                })
            },
            addOne() {
                this.id = ""
                this.type = ""
                this.name=""
                this.pic = ""
                this.commodityId = ""
                this.mark = ""


                this.dialogVisible = true
                this.insert = 1
            },
            dosubmit() {
                let ad = {
                    id: this.id,
                    type: this.type,
                    name:this.name,
                    pic: this.pic,
                    commodityId: this.commodityId,
                    mark: this.mark
                }
                if (this.insert == 0) {
                    axios.put(this.$store.state.base_url + '/ad/', ad).then(res => {
                        if (res.data.code == "SUCCESS") {
                            this.dialogVisible = false
                            this.getalldata()
                        } else {
                            this.$notify.error({
                                title: '修改失败',
                                message: this.$createElement('i', {
                                    style: 'color: teal'
                                }, res.data.message),
                            });
                        }
                    })
                } else {
                    axios.post(this.$store.state.base_url + '/ad/', ad).then(res => {
                        if (res.data.code == "SUCCESS") {
                            this.dialogVisible = false
                            this.getalldata()
                        } else {
                            this.$notify.error({
                                title: '新增失败',
                                message: this.$createElement('i', {
                                    style: 'color: teal'
                                }, res.data.message),
                            });
                        }
                    })
                }
            },
            getalldata() {
                axios.get(this.$store.state.base_url + '/ad/').then(data => {
                    let ads = data.data.data
                    console.log(ads)
                    this.rotationData = []
                    this.normalData = []
                    this.bestsellData = []
                    this.elaborateData = []

                    for (const iterator of ads) {
                        if (iterator.type == "rotation") {
                            this.rotationData.push(iterator)
                        } else if (iterator.type == "normal") {
                            this.normalData.push(iterator)
                        } else if (iterator.type == "bestsell") {
                            this.bestsellData.push(iterator)
                        } else if (iterator.type == "elaborate") {
                            this.elaborateData.push(iterator)
                        }
                    }
                })
            },
            handlePreview(response) {
                console.log(response)
                let url = this.$store.state.upload_base_url.replace('index.php', '') + response.data
                this.pic = url
            },
        },

        mounted() {
            this.getalldata()
        }
    }
</script>


<style scoped>

</style>